/**
 * @file     guessNumber.js
 * @brief    [374. 猜数字大小](https://leetcode.cn/problems/guess-number-higher-or-lower/description/)
 * @author   Zhu
 * @date     2023-02-13 23:06
 */

/**
 * Forward declaration of guess API.
 * @param {number} num   your guess
 * @return 	     -1 if num is higher than the picked number
 *			      1 if num is lower than the picked number
 *               otherwise return 0
 * var guess = function(num) {}
 */

/**
 * @param {number} n
 * @return {number}
 */
var guessNumber = function (n) {
    let left = 1;
    let right = n;
    let mid;
    do {
        mid = Math.floor(left / 2 + right / 2);
        result = guess(mid);
        if (result > 0) {
            left = mid + 1;
        } else if (result < 0) {
            right = mid;
        } else {
            return mid;
        }
    } while (left < right);

    return left;
};
